package com.kaoqin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kaoqin.mapper.SignLogMapper;
import com.kaoqin.model.SignLog;
import com.kaoqin.model.SignLogVO;
import com.kaoqin.service.SignLogService;
import com.kaoqin.service.SignRuleService;
import com.kaoqin.util.Constants;
import com.kaoqin.util.DateUtil;
import com.kaoqin.util.SysUtils;

@Service
@Transactional
public class SignLogServiceImpl implements SignLogService {
	@Resource
	private SignLogMapper mapper;
	@Autowired
	private SignRuleService signRuleService;

	@Override
	public List<SignLog> findAll(SignLog signLog) {
		return mapper.findAll(signLog);
	}
	@Override
	public void saveSingLog(SignLog signLog) throws RuntimeException {
		Long sId = signLog.getId();
		if(sId!=null){
			mapper.updateOne(signLog);
		}else {
			mapper.insertOne(signLog);
		}
	}
	public void packageSignDetail(SignLog signLog, String dataType) throws RuntimeException{
		SignLog paramSignLog = new SignLog();
		paramSignLog.seteId(signLog.geteId());
		paramSignLog.setSignDate(DateUtil.getCurrentTimestamp(null, DateUtil.DATE_DEFAULT_FORMAT));
		paramSignLog.setOrderSql(" order by sign_date,id desc");
		List<SignLog> currentData = mapper.findAll(paramSignLog);
		// 如果当日没有签到数据就新增否则更新下班时间
		if (!SysUtils.isNotList(currentData)) {
			if (Constants.TO_WORK.equalsIgnoreCase(dataType)) {
				signLog.setSignToWork(DateUtil.getCurrentTimestamp());
				signRuleService.updateSignDateByRule(signLog, dataType);
				mapper.insertOne(signLog);
			}
		}
		// 如果存在上班时间则取出最后上班日期并且不存在下班日期的数据进行更新
		if (SysUtils.isNotList(currentData)) {
			SignLog dataSL = currentData.get(0);
			if (Constants.TO_WORK.equalsIgnoreCase(dataType)) {
				if (dataSL.getSignToWork() != null && dataSL.getSignOffWork() != null) {
					signLog.setSignToWork(DateUtil.getCurrentTimestamp());
					signRuleService.updateSignDateByRule(signLog, dataType);
					mapper.insertOne(signLog);
				}
				if (dataSL.getSignToWork() == null && dataSL.getSignOffWork() != null) {
					signLog.setSignToWork(DateUtil.getCurrentTimestamp());
					signRuleService.updateSignDateByRule(signLog, dataType);
					mapper.insertOne(signLog);
				}
			}
			// 如果是当日最新数据
			if (Constants.OFF_WORK.equalsIgnoreCase(dataType)) {
				dataSL.setSignOffWork(DateUtil.getCurrentTimestamp());
				signRuleService.updateSignDateByRule(dataSL, dataType);
				mapper.updateOne(dataSL);
			}
		}
	}
	
	@Override
	public List<SignLogVO> findSignDataToExcel(SignLog signLog) {
		List<SignLogVO> findSignDataExcel = mapper.findSignDataExcel(signLog);
		Map<Long, Long> countTimeMap = new HashMap();
		for (SignLogVO slog : findSignDataExcel) {
			Long geteId = slog.geteId();
			Long shichang = getTime(slog);
			if (countTimeMap.containsKey(geteId)) {
				Long time = countTimeMap.get(geteId);
				time += shichang;
				countTimeMap.put(geteId, time);
			} else {
				countTimeMap.put(geteId, shichang);
			}
		}
		for (SignLogVO slog : findSignDataExcel) {
			if (slog.getSignToWork() == null || slog.getSignOffWork() == null) {
				slog.setSignShiChang("0");
				continue;
			}
			slog.setSignCountTime(DateUtil.getHourTime(countTimeMap.get(slog.geteId())));
			slog.setSignShiChang(DateUtil.formatTime(getTime(slog)));
		}
		return findSignDataExcel;
	}
	private Long getTime(SignLogVO slog) {
		Long endTime = slog.getSignOffWork() == null ? 0 : slog.getSignOffWork().getTime();
		Long beginTime = slog.getSignToWork() == null ? 0 : slog.getSignToWork().getTime();
		if(endTime==0 || beginTime==0) {
			return 0L;
		}
		return endTime - beginTime;
	}
    @Override
    public void updateSignLog(SignLog signLog) throws RuntimeException {
        mapper.updateSignLog(signLog);        
    }
	@Override
	public PageInfo<SignLogVO> findHistorySignLogList(SignLogVO signLog) {
		PageHelper.startPage(signLog.getPageNum(), signLog.getPageSize());
		List<SignLogVO> findHistorySignLogList = mapper.findHistorySignLogList(signLog);
		PageInfo<SignLogVO> page = new PageInfo<SignLogVO>(findHistorySignLogList);
		return page;
	}
	@Override
	public Integer findHistorySignLogListCount(SignLogVO signLog) {
		return mapper.findHistorySignLogListCount(signLog);
	}
	@Override
	public Integer delSignLog(Integer slId) {
		return mapper.delSignLog(slId);
	}
	@Override
	public SignLog findSignLogById(Integer id) {
		return  mapper.findSignLogById(id);
	}
}
